package controller;

import entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.StudentService;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Resource
    private StudentService service;
//    insert
    @RequestMapping("/insert.do")
    public ModelAndView addStudent(Student student){
        ModelAndView modelAndView = new ModelAndView();
        String tips="注册失败";
//        调用service处理servlet
        int num = service.addStudent(student);
        if (num>0){
            tips="学生->"+student.getName()+"<-注册成功";
        }
        modelAndView.addObject("tips",tips);
//        指定结果界面
        modelAndView.setViewName("result");
        return modelAndView;
    }

    @RequestMapping("/query.do")
    @ResponseBody
    public List<Student> queryStudent(){
        return service.queryStudent();
    }

    @RequestMapping("/update.do")
    public ModelAndView refreshStudent(Student student){
        ModelAndView modelAndView = new ModelAndView();
        String tips="操作失败";
        int num=service.reStudent(student);
        if (num>0){
            tips="更新成功："+student.getName();
        }
        modelAndView.addObject("tips",tips);
        modelAndView.setViewName("update");

        return modelAndView;
    }
    @RequestMapping("/delete.do")
    public ModelAndView deleteStudent(Student student){
        ModelAndView modelAndView = new ModelAndView();
        String tips="操作失败";
        int num=service.delStudent(student);
        if (num>0){
            tips="删除成功："+student.getName();
        }
        modelAndView.addObject("tips",tips);
        modelAndView.setViewName("delete");

        return modelAndView;
    }

    @RequestMapping("/thymeleaf.do")
    public ModelAndView  ThymeleafController() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("helloThymeleaf","Hello,Thymeleaf");
        modelAndView.setViewName("thymeleaf");
        return modelAndView;
    }


}
